<template>
    <div class="pin-list">

      <div v-if="pinList.length > 0" class="grid-container">
        <div v-for="pin in paginatedList" :key="pin.id" class="grid-item">
      
          <div v-if="pin.jsonContent" class="grid-info">
        <p><strong>JSON内容:</strong> {{ pin.jsonContent }}</p>
      </div>
     
          <p><strong>上传日期:</strong> {{ pin.date_pinned }}</p>
          <p><strong>删除日期:</strong> {{ pin.date_unpinned }}</p>
         
          <p><strong>大小:</strong> {{ pin.size }}</p>
          <button @click="navigateToForm(pin)">初始化nft</button>
        </div>
      </div>
      <div v-else>
        <p>加载中...</p>
      </div>
      <div class="pagination" style="grid-column-start: 1; grid-column-end: 3; justify-self: center;">
      <div class="pagination">
        <button @click="changePage(1)" :disabled="currentPage === 1">首页</button>
        <button @click="changePage(currentPage - 1)" :disabled="currentPage === 1">上一页</button>
        <button @click="changePage(currentPage + 1)" :disabled="currentPage === totalPages">下一页</button>
        <button @click="changePage(totalPages)" :disabled="currentPage === totalPages">尾页</button>
      </div>
    </div>
    </div>
  </template>
  
  
  
  
  <script setup>
 import { ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';

const pinList = ref([]);
const currentPage = ref(1);
const pageSize = ref(4);
const router = useRouter();

const totalPages = computed(() => Math.ceil(pinList.value.length / pageSize.value));
const paginatedList = computed(() => {
  const startIndex = (currentPage.value - 1) * pageSize.value;
  const endIndex = startIndex + pageSize.value;
  return pinList.value.slice(startIndex, endIndex);
});


const fetchJsonContent = async (pin) => {
  try {
    const response = await fetch('https://ipfs.io/ipfs/' + pin.ipfs_pin_hash);
    const jsonContent = await response.json();
    pin.jsonContent = JSON.stringify(jsonContent, null, 2); // 格式化JSON字符串以便阅读
  } catch (error) {
    console.error('获取JSON内容时出错:', error);
  }
};

  onMounted(() => {
  const options = {
    method: 'GET',
    headers: {
        Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mb3JtYXRpb24iOnsiaWQiOiJmMjY5NWYwMi00ZTcwLTQ5MmEtODdkZS0yNzk5OWU5ODdiZmEiLCJlbWFpbCI6IjE5NTczNzY2NTRAcXEuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsInBpbl9wb2xpY3kiOnsicmVnaW9ucyI6W3siaWQiOiJGUkExIiwiZGVzaXJlZFJlcGxpY2F0aW9uQ291bnQiOjF9LHsiaWQiOiJOWUMxIiwiZGVzaXJlZFJlcGxpY2F0aW9uQ291bnQiOjF9XSwidmVyc2lvbiI6MX0sIm1mYV9lbmFibGVkIjpmYWxzZSwic3RhdHVzIjoiQUNUSVZFIn0sImF1dGhlbnRpY2F0aW9uVHlwZSI6InNjb3BlZEtleSIsInNjb3BlZEtleUtleSI6IjU5ODNlMmExNjQ0ZWQwMGUxMTc0Iiwic2NvcGVkS2V5U2VjcmV0IjoiMWE2Mzk2ZGQ0ZjMyOTM4OWVhMGRlMjIxZWVkYzcxMzJlN2U1MzU3OTZlNjFjOTQ5ZTc4NGQ5ZjNkYjZhMTdkMiIsImlhdCI6MTcxMjI4MTUxOH0.oGVPWgzMcv4H-pu6gxhVdCJOBq7dQi-Amjx8YMiKi_I',
    }
  };

  fetch('https://api.pinata.cloud/data/pinList?metadata[name]=json', options)
    .then(response => response.json())
    .then(data => {
      pinList.value = data.rows;
      pinList.value.forEach(pin => fetchJsonContent(pin));
    })
    .catch(err => console.error('请求Pinata API时出错:', err));


    
    
});
  
function navigateToForm(pinData) {
  
  //console.log(pinData);
  pinData = JSON.stringify(pinData)
  router.push({ path: '/mintcollection', query: { 'pinData': pinData } });
  //router.push({ name: 'FormPage', params: { 'pinData': pinData } });
}

function changePage(pageNumber) {
  currentPage.value = pageNumber;
}

  </script>
  
      
  <style scoped>


.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 20px;
  width: 100%;
  /* 新增样式，使分页按钮组跨越两列 */
  grid-column: 1 / -1;
  /* 其他样式保持不变 */
}
  .pin-list {
    display: grid;
    grid-template-columns: repeat(1, minmax(0, 1fr)); /* 创建两列，每列宽度相等 */
    grid-gap: 20px; /* 设置网格间隙 */
    max-width: 1000px;
    margin: 40px auto;
    padding: 20px;
    background-color: #f0f0f0;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  }
  
  .grid-container {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr)); /* 创建两列，每列宽度相等 */
    grid-gap: 20px; /* 设置网格间隙 */
    width: 100%;
  }
  
  .grid-item {
  background-color: #ffffff;
  border-radius: 4px;
  padding: 15px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  width: 100%; /* 设置宽度为80% */
}
.grid-info{
	width:calc(100% - 15px);
	word-wrap: break-word;
}
  h3 {
    color: #0056b3;
    margin-bottom: 10px;
  }
  
  p {
    color: #666666;
    line-height: 1.6;
  }
  
  button {
    background-color: #0056b3;
    color: #ffffff;
    padding: 10px 15px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.3s;
  }
  
  button:hover {
    background-color: #004080;
  }
  
  .pin-image {
    width: 90%; /* 调整图片宽度为90% */
    border-radius: 4px;
    margin-top: 15px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  }
  
  .pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 20px;
    width: 100%;
    max-width: 300px; /* 限制翻页按钮组的最大宽度 */
    margin: 20px auto; /* 居中显示 */
  }
  
  .pagination button {
    padding: 5px 10px; /* 缩小按钮大小 */
    margin: 0 5px;
  }
  </style>
  
  